﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;


class MenuBase : EnvBase
{
    private Boolean isMainScreen = true;
    private Boolean isSettingsScreen = false;
    private Boolean isCreditsScreen = false;
    private Boolean isControlsScreen = false;

    private ArrayList mainMenuArrowPositions;
    private ArrayList controlsMenuArrowPositions;
    private ArrayList settingsMenuArrowPositions;
    private ArrayList creditsMenuArrowPositions;
    private ArrayList currentArrowArrayList;

    private int currentMainMenuButtonIndex = 0;
    private int currentControlsMenuButtonIndex = 0;
    private int currentSettingsMenuButtonIndex = 0;
    private int currentCreditsMenuButtonIndex = 0;


    // Arrows

    Rectangle mainMenuArrowsRectangle = new Rectangle(0, 0, 290, 59);
    Vector2 mainMenuArrowsPosition = new Vector2(0, 0);
    Animation arrowsAnimation;
    
    // Main Screen

    Rectangle mainMenuScreenRectangle = new Rectangle(0, 0, 1024, 576);
    Vector2 mainMenuScreenPosition = new Vector2(0, 0);
    Animation mainMenuScreenAnimation;

    // Settings Screen

    Rectangle settingsScreenRectangle = new Rectangle(0, 0, 1024, 576);
    Vector2 settingsScreenPosition = new Vector2(0, 0);
    Animation settingsScreenAnimation;
    
    
    
    // creditsScreen

    Rectangle creditsScreenRectangle = new Rectangle(0, 0, 1024, 576);
    Vector2 creditsScreenPosition = new Vector2(0, 0);
    Animation creditsScreenAnimation;
    
    

    // controlsScreen

    Rectangle controlsScreenRectangle = new Rectangle(0, 0, 1024, 576);
    Vector2 controlsScreenPosition = new Vector2(0, 0);
    Animation controlsScreenAnimation;
    

    public MenuBase()
    {
        loadAssets();
        loadArrowPositions();
        currentArrowArrayList = mainMenuArrowPositions;
        Game.getGame().getSoundManager().stopSound();
        Game.getGame().getSoundManager().playSound(((AudioAsset)Game.getGame().getAssetLibrary().getAudio(AssetLibrary.audioType.MENU_MUSIC).get("mainMenuMusic")).sound, 0.8f);
    }

    private void loadAssets()
    {
        // load main screen
        mainMenuScreenAnimation = new Animation("mainScreen", 0, false);
        mainMenuScreenAnimation.addFrame(
            new Frame(
                Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.MENU).get("mainScreen"), 
                mainMenuScreenRectangle, 
                0, 
                new Vector2(0,0)
                ));
         
        // load controls
        controlsScreenAnimation = new Animation("controlsScreen", 0, false);
        controlsScreenAnimation.addFrame(
            new Frame(
                Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.MENU).get("controlsScreen"),
                mainMenuScreenRectangle,
                0,
                new Vector2(0, 0)
                ));

        // load settings
        settingsScreenAnimation = new Animation("settingsScreen", 0, false);
        settingsScreenAnimation.addFrame(
            new Frame(
                Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.MENU).get("settingsScreen"),
                settingsScreenRectangle,
                0,
                new Vector2(0, 0)
                ));
        
        // load credits
        creditsScreenAnimation = new Animation("creditsScreen", 0, false);
        creditsScreenAnimation.addFrame(
            new Frame(
                Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.MENU).get("creditsScreen"),
                mainMenuScreenRectangle,
                0,
                new Vector2(0, 0)
                ));

        // load arrows
        arrowsAnimation = new Animation("arrows", 0, false);
        arrowsAnimation.addFrame(
            new Frame(
                Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.MENU).get("mainScreenArrows"),
                mainMenuArrowsRectangle,
                0,
                new Vector2(0, 0)
                ));

        

    }

    private void loadArrowPositions()
    {
        // main menu
        mainMenuArrowPositions = new ArrayList();
        mainMenuArrowPositions.Add(new Vector2(372, 319));
        mainMenuArrowPositions.Add(new Vector2(372, 375));
        mainMenuArrowPositions.Add(new Vector2(372, 435));
        mainMenuArrowPositions.Add(new Vector2(372, 495));

        // settings menu
        settingsMenuArrowPositions = new ArrayList();
        settingsMenuArrowPositions.Add(new Vector2(372, 296));
        settingsMenuArrowPositions.Add(new Vector2(372, 351));

        // credits menu
        creditsMenuArrowPositions = new ArrayList();
        creditsMenuArrowPositions.Add(new Vector2(367, 491));

        // controls menu
        controlsMenuArrowPositions = new ArrayList();
        controlsMenuArrowPositions.Add(new Vector2(373, 505));

        // arows
        
    }

    private void updateArrowPosition(char direction)
    {
        if (direction == 'u')
        {
            if(isMainScreen)
            {
                currentMainMenuButtonIndex--;
                if (currentMainMenuButtonIndex < 0)
                {
                    currentMainMenuButtonIndex = mainMenuArrowPositions.Count - 1;
                }
            } 
            else if(isSettingsScreen)
            {
                currentSettingsMenuButtonIndex--;
                if (currentSettingsMenuButtonIndex < 0)
                {
                    currentSettingsMenuButtonIndex = settingsMenuArrowPositions.Count - 1;
                }
            } 
        }
        if (direction == 'd')
        {
            if (isMainScreen)
            {
                currentMainMenuButtonIndex++;
                if (currentMainMenuButtonIndex > mainMenuArrowPositions.Count - 1)
                {
                    currentMainMenuButtonIndex = 0;
                }
            }
            else if (isSettingsScreen)
            {
                currentSettingsMenuButtonIndex++;
                if (currentSettingsMenuButtonIndex > settingsMenuArrowPositions.Count - 1)
                {
                    currentSettingsMenuButtonIndex = 0;
                }
            } 
        }
        Game.getGame().getSoundManager().playOneSoundEffect(((AudioAsset)Game.getGame().getAssetLibrary().getAudio(AssetLibrary.audioType.SOUND_EFFECT).get("menu_click")).sound, 1.0f);
    }

    private void executeButton()
    {
        if (isMainScreen)
        {
            if (currentMainMenuButtonIndex == 0)
            {
                // Fading into our Level Env. 2000 = 2 seconds to fade.
                m_parentStack.push(new FadeOutEnv(new LevelBase(Levels.List.kEdLevel), 2000));
            }
            else if (currentMainMenuButtonIndex == 1)
            {
                isMainScreen = false;
                isSettingsScreen = true;
            }
            else if (currentMainMenuButtonIndex == 2)
            {
                isMainScreen = false;
                isCreditsScreen = true;
            }
            else if (currentMainMenuButtonIndex == 3)
            {
                m_parentStack.popAll();
            }
        }
        else if (isSettingsScreen)
        {
            if (currentSettingsMenuButtonIndex == 0)
            {
                isSettingsScreen = false;
                isControlsScreen = true;
            }
            else if (currentSettingsMenuButtonIndex == 1)
            {
                isSettingsScreen = false;
                isMainScreen = true;
            }
        }
        else if (isControlsScreen)
        {
            isMainScreen = true;
            isControlsScreen = false;
        }
        else if (isCreditsScreen)
        {
            isMainScreen = true;
            isCreditsScreen = false;
        }

        // Reset menu choices for the next time we visit this screen.
        currentMainMenuButtonIndex = 0;
        currentControlsMenuButtonIndex = 0;
        currentSettingsMenuButtonIndex = 0;
        currentCreditsMenuButtonIndex = 0;

        Game.getGame().getSoundManager().playOneSoundEffect(((AudioAsset)Game.getGame().getAssetLibrary().getAudio(AssetLibrary.audioType.SOUND_EFFECT).get("menu_select")).sound, 1.0f);
    }

    public override void render()
    {
        base.render();
        if (isMainScreen)
        {
            mainMenuScreenAnimation.render(new Vector2(0, 0), 0.06f);
            arrowsAnimation.render((Vector2)mainMenuArrowPositions[currentMainMenuButtonIndex], 0.05f);
            
        }
        else if (isSettingsScreen)
        {
            settingsScreenAnimation.render(new Vector2(0, 0), 0.06f);
            arrowsAnimation.render((Vector2)settingsMenuArrowPositions[currentSettingsMenuButtonIndex], 0.05f); 
        }
        else if (isCreditsScreen)
        {
            creditsScreenAnimation.render(new Vector2(0, 0), 0.06f);
            arrowsAnimation.render((Vector2)creditsMenuArrowPositions[currentCreditsMenuButtonIndex], 0.05f); 
        }
        else if (isControlsScreen)
        {
            controlsScreenAnimation.render(new Vector2(0, 0), 0.06f);
            arrowsAnimation.render((Vector2)controlsMenuArrowPositions[currentControlsMenuButtonIndex], 0.05f); 
        } 
    }

    public override void update(double dt)
    {
        InputManager im = InputManager.getSingleton();
        im.update();

        if (im.getJustPressedControlState(InputManager.DOWN_BUTTON))
        {
            updateArrowPosition('d');
        }
        else if (im.getJustPressedControlState(InputManager.UP_BUTTON))
        {
            updateArrowPosition('u');
        }

        if (im.getJustPressedControlState(InputManager.JUMP_BUTTON))
        {
            executeButton();
        }

#if DEBUG
        if(isMainScreen)
        {
            if(im.getJustPressedControlState(InputManager.BACK_BUTTON))
            {
                m_parentStack.popAll();
            }
            else if(im.getJustPressedControlState(InputManager.PAUSE_BUTTON))
            {
                m_parentStack.push(new EditorLoader());
            }
        }
#endif

        base.update(dt);

    }

}